<?php

namespace App\Http\Controllers\Business;

use App\Models\Dictionary;
use App\Models\Manager;
use App\Models\Order;
use App\Models\OrderLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Session;
use Illuminate\Support\Facades\Validator;

class OrderController extends Controller
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $product = Dictionary::where(['pid'=>1])
            ->get();
//        dump(Session::get('manager'));
        return view('business.Order.index',compact('product'));
    }

    /**
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function store(Request $request)
    {
        $validate = Validator::make($request->all(),[
            'userName' => 'required|min:2',
            'userPhone' => 'required|min:11|max:11',
//            'userIdentity' => 'required|min:15|max:18',
//            'loanNum' => 'required',
//            'loanType' => 'required'
        ]);
        if(!$validate->fails()) {
            $applyType = Dictionary::where(['name'=>$request->loanType])->first();
            $companyInfo = Manager::with(['parent'=>function($query){
                $query->select(['id','name','company']);
            }])
                ->find(Session::get('manager.id'),['id','name','company','parent_mid']);
            if(!empty($applyType) && !empty($companyInfo)){
                $order = new Order();
                $userInfo = ['phone'=>$request->userPhone,'idcard'=>$request->userIdentity?$request->userIdentity:'','customer'=>$request->userName,'apply_type'=>$applyType->id,'apply_amount'=>$request->loanNum,'parentName'=>$request->parentName,'parentPhone'=>$request->parentPhone,'seManga'=>$request->seManga,'baHeng'=>$request->baHeng,'mzu'=>$request->mzu,'height'=>$request->height,'weight'=>$request->weight];
                $order->userinfo = $userInfo;
                $order->mid = Session::get('manager.id');
                $channle_info = ['manager'=>$companyInfo->name,'my_company'=>$companyInfo->company];
                if($companyInfo->parent)
                {
                    array_merge($channle_info,['parent_company'=>$companyInfo->parent['company'],'parent_manager'=>$companyInfo->prent['name']]);
                }
                $order->channel_info = $channle_info;
                $order->status = 0;
                $order->confirm_loan = [];
                $insert = $order->save();
                $id = $order->id;
//                if($insert)
//                {
                    OrderLog::create(['name'=>Session::get('manager.name'),'operate_type'=>1,'order_id'=>$id,'operator_id'=>Session::get('manager.id')]);
//                }else{
//                    $this->set('code',2);
//                    $this->set('message','系统错误');
//                }
            }else{
                $this->set('code',3);
                $this->set('message','系统错误');
            }
        }
        else{
            $this->set('code',4);
            $this->set('message',$validate->fails());
        }
        return $this->ajaxResponse();
    }

    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * 成功页面
     */
    public function success(){
        return view('business.Order.success');
    }
}
